package com.myfactoryMethod;

import java.util.Map;

/**
 * 工厂方法模式
 * @author yulipeng
 *
 */
public class PhoneFactory {

	/**
	 * 根据手机品牌获取具体手机
	 * @param type
	 * @return
	 */
	public Phone getPhone(String type){
		if ("huawei".equals(type)) {
			return new HuaweiPhone();
		} else if ("apple".equals(type)) {
			return new ApplePhone();
		}
		return null;
	}
	
	/**
	 * 根据反射（reflect）来获取具体手机
	 * @param type
	 * @return
	 */
	public Phone getPhoneByClass(String className){
		try {
			Phone phone = (Phone)Class.forName(className).newInstance();
			return phone;
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 根据反射（reflect）来获取具体手机(使用配置文件)
	 * @param type
	 * @return
	 */
	public Phone getPhoneByPropClass(String className){
		try {
			PropertiesReader pr = new PropertiesReader();
			Map<String, String> map = pr.getProperties();
			Phone phone = (Phone)Class.forName(map.get(className)).newInstance();
			return phone;
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
}
